package link;

/**
 * @author zhangguisong
 * @date 2022-10-24 15:52
 */
public class LinkedList<E> {
    private Node<E> head;
    private Node<E> tail;
    private int size;

    public LinkedList() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    private static class Node<E> {
        private Node<E> prev;
        private Node<E> next;
        private E value;

        public Node() {
        }

        public Node(Node<E> prev, Node<E> next, E value) {
            this.prev = prev;
            this.next = next;
            this.value = value;
        }

        public Node<E> getPrev() {
            return prev;
        }

        public void setPrev(Node<E> prev) {
            this.prev = prev;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

        public E getValue() {
            return value;
        }

        public void setValue(E value) {
            this.value = value;
        }
    }

    public boolean add(E e) {
        Node<E> newNode = new Node<>(tail, null, e);
        Node<E> temp = tail;
        tail = newNode;
        if (temp == null) {
            head = newNode;
        } else {
            temp.next = newNode;
        }
        size++;
        return true;
    }

    @Deprecated
    public boolean remove(E e) {
        Node<E> node = head;
        while (node != null) {
            if (node.value == e) {
                if (node == tail) {
                    tail = node.prev;
                    tail.prev = node.prev.prev;
                    tail.next = null;
                } else if (node == head && head.next == null) {
                    head = null;
                    tail = null;
                } else if(node == head && head.next != null){
                    node.next.prev=null;
                    head=node.next;
                    node.prev=null;
                    node.next=null;
                }else {
                    node.next.prev = node.prev;
                    node.prev.next = node.next;
                    node.prev = null;
                    node.next = null;
                }
                size--;
                return true;
            }
            node = node.next;
        }
        return false;
    }



    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList();
        linkedList.add("Z");
        linkedList.add("G");
        linkedList.add("S");
        linkedList.remove("Z");
        System.out.println(linkedList);

    }
}
